package org.hyn.gateway.assist.application;

import org.hyn.gateway.assist.config.GatewayServiceProperties;
import org.hyn.gateway.assist.service.RegisterGatewayService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * @author franyhu
 * @description 网关应用，与spring链接，调用网关注册和接口拉区
 * @date 2025/3/26
 */
public class GatewayApplication implements ApplicationListener<ContextRefreshedEvent> {

    private final GatewayServiceProperties properties;
    private final RegisterGatewayService registerGatewayService;

    public GatewayApplication(GatewayServiceProperties properties, RegisterGatewayService registerGatewayService) {
        this.properties = properties;
        this.registerGatewayService = registerGatewayService;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 注册网关服务；每一个用于转化HTTP协议泛化调用到RPC接口的网关都是一个算力，这些算力需要注册网关配置中心
        registerGatewayService.doRegister(
                properties.getAddress(),
                properties.getGroupId(),
                properties.getGatewayId(),
                properties.getGatewayName(),
                properties.getGatewayAddress()
        );
    }
}
